package com.customization.huangwm.workflowNodeAction.SW003;

import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.DateTools;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Author Huangwm
 * @Date 2022-12-07
 * @Version 1.0
 * @Description TODO 物料转仓 1、提交时校验仓库库存 2、提交后冻结仓库库存 3、归档后将数量写入新仓库并且更新关联资产的仓库
 **/
public class WareHouseConvert implements Action {
    private LogTool log = new LogTool("/log/SW003/huangwm/WfAction/WareHouseConvert", false);
    private DBTools dbTools = new DBTools();
    private DateTools dateTools = new DateTools();

    public String type;//submit_create,submit_review,submit_end

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String nodeType = requestInfo.getRequestManager().getNodetype();
        String src = requestInfo.getRequestManager().getSrc();
        int creater = requestInfo.getRequestManager().getCreater();
        log.writeLog("物料转仓流程>>>" + requestid + ">>" + nodeType + ">>" + src + ">>" + type);
        if (type.equals("") || (!type.equals("submit_create") && !type.equals("submit_review") && !type.equals("submit_end") && !type.equals("submit_reject"))) {
            requestInfo.getRequestManager().setMessageid(requestid);
            requestInfo.getRequestManager().setMessagecontent("未獲取到Action參數type或參數值不正確,請聯繫管理員檢查後台接口動作設置!;");
            return FAILURE_AND_CONTINUE;
        }

        RecordSet rs = new RecordSet();
        String tableName = requestInfo.getRequestManager().getBillTableName();
        String billid = requestInfo.getRequestManager().getBillid() + "";
        String sql = "select * from "+tableName+"_dt1 where mainid="+billid;
        RecordSet exis = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        if(src.equals("reject")){

        }else{
            int index =1;
            if(type.equals("submit_create")){

            }else if(type.equals("submit_end")){
                try{
                    rs.execute(sql);
                    while (rs.next()){
                        String assetCode = rs.getString("assetCode");
                        Double applicationQuantity = rs.getDouble("applicationQuantity");
                        String oldWareHouse = rs.getString("oldWareHouse");
                        String newWareHouse = rs.getString("newWareHouse");
                        String mainAssetCode = rs.getString("mainAssetCode");
                        //原倉庫操作
                        String sql1 = "select * from uf_003_WarehouseNum where assetcode="+assetCode+" and wareHouse="+oldWareHouse;
                        log.writeLog("原倉庫操作>"+sql1);
                        exis.execute(sql1);
                        if(exis.next()){
                            Double num3 = exis.getDouble("num3");//仓库剩余库存
                            int thisID = exis.getInt("id");
                            if(num3<applicationQuantity){
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("第"+index+"行物料倉庫庫存餘量不足,剩餘["+num3+"]");
                                return FAILURE_AND_CONTINUE;
                            }else{
                                String up = "update uf_003_WarehouseNum set num3=num3-"+applicationQuantity+",num1=num1-"+applicationQuantity+" where id="+thisID;
                                log.writeLog("原倉庫库存减少>"+up);
                                boolean result1 = rst.executeUpdate(up);
                                if(!result1){
                                    rst.rollback();
                                    requestInfo.getRequestManager().setMessageid(requestid);
                                    requestInfo.getRequestManager().setMessagecontent("第"+index+"行物料轉倉扣減當前倉庫庫存失敗");
                                    return FAILURE_AND_CONTINUE;
                                }
                            }
                        }else{
                            rst.rollback();
                            requestInfo.getRequestManager().setMessageid(requestid);
                            requestInfo.getRequestManager().setMessagecontent("第"+index+"行物料未查詢到倉庫庫存,請確認選擇的原倉庫是否存在庫存");
                            return FAILURE_AND_CONTINUE;
                        }

                        //轉倉操作
                        String sql2 = "select * from uf_003_WarehouseNum where assetcode="+assetCode+" and wareHouse="+newWareHouse;
                        log.writeLog("轉倉操作>"+sql2);
                        exis.execute(sql2);
                        if(exis.next()){
                            int thisID = exis.getInt("id");
                            //存在倉庫直接纍加庫存
                            String up1 = "update uf_003_WarehouseNum set num3=num3+"+applicationQuantity+",num1=num1+"+applicationQuantity+" where id="+thisID;
                            log.writeLog("存在倉庫直接纍加庫存>"+up1);
                            boolean result1 = rst.executeUpdate(up1);
                            if(!result1){
                                rst.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("第"+index+"行物料轉倉新增轉倉倉庫庫存失敗");
                                return FAILURE_AND_CONTINUE;
                            }
                        }else {
                            //沒有新倉庫記錄  插入
                            String s = "insert into uf_003_WarehouseNum" +
                                    "(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID"+
                                    ",assetCode,wareHouse,num1,num2,num3,num4) " +
                                    "values(?,?,?,?,?,?,?,?,?,?,?,?)";
                            String modedatacreatedate = dateTools.getCurrentDate();
                            String modedatacreatetime = dateTools.getCurrentTime();
                            String uuid = dateTools.getUUID();
                            String uf_003_WareHouseNum_Modeid = new BaseBean().getPropValue("SW003", "uf_003_WareHouseNum_Modeid");
                            if(uf_003_WareHouseNum_Modeid.isEmpty()){
                                rst.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("配置文件SW003.peoperties中缺少uf_003_WareHouseNum_Modeid配置項");
                                return FAILURE_AND_CONTINUE;
                            }
                            Object[] objects = new Object[]{
                                    uf_003_WareHouseNum_Modeid, 1, 0, modedatacreatedate, modedatacreatetime, uuid,
                                    assetCode,newWareHouse,applicationQuantity,0,applicationQuantity,0
                            };
                            log.writeLog("沒有新倉庫記錄  插入");
                            log.writeLog(JSONObject.toJSONString(objects));
                            boolean result1 = rst.executeUpdate(s,objects);
                            if(!result1){
                                rst.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("第"+index+"行物料轉倉生成新倉庫庫存失敗");
                                return FAILURE_AND_CONTINUE;
                            }
                        }

                        if(!mainAssetCode.isEmpty()){
                            //處理關聯資產的倉庫
                            String up3 = "update uf_003_AssetInfo2 set wareHouse="+newWareHouse+" where id in("+mainAssetCode+")";
                            log.writeLog("處理關聯資產的倉庫>"+up3);
                            boolean result2 = rst.executeUpdate(up3);
                            if(!result2){
                                rst.rollback();
                                requestInfo.getRequestManager().setMessageid(requestid);
                                requestInfo.getRequestManager().setMessagecontent("處理關聯資產的倉庫失敗");
                                return FAILURE_AND_CONTINUE;
                            }
                        }

                        index ++;
                    }
                    rst.commit();
                    return SUCCESS;
                }catch (Exception e){
                    rst.rollback();
                    e.printStackTrace();
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("轉倉歸檔異常 "+e.getMessage());
                    return FAILURE_AND_CONTINUE;
                }

            }
        }
        return SUCCESS;
    }
}
